package com.zhb.cloud.condig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.jta.JtaTransactionManager;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;

@Configuration
public class DataSourceConfig {
	public @Bean JtaTransactionManager jtaTransactionManager() throws Exception {
		JtaTransactionManager jtaTransactionManager = new JtaTransactionManager();
		jtaTransactionManager.setDefaultTimeout(6000);
		jtaTransactionManager.setTransactionManager(atomikosTransactionManager());
		jtaTransactionManager.setUserTransaction(userTransactionImp());
		return jtaTransactionManager;
	}

	public @Bean UserTransactionManager atomikosTransactionManager() throws Exception {
		UserTransactionManager userTransactionManager = new UserTransactionManager();
		userTransactionManager.setForceShutdown(true);
		return userTransactionManager;
	}

	@Bean(name = "secondaryDataSource")
	public UserTransactionImp userTransactionImp() throws Exception {
		UserTransactionImp userTransactionImp = new UserTransactionImp();
		userTransactionImp.setTransactionTimeout(30);
		return userTransactionImp;
	}

}